// 这是一个 addMenuPlus 配置文件
// 请到 http://ywzhaiqi.github.io/addMenu_creator/ 生成配置文件
css("#context-copylink {display: none}");

page({
    label: "有道翻译",
    insertBefore: "snaplinksMenuEntry",
    condition: "normal",
    oncommand : function (){
        var s = content.document.createElement('script');
        s.type = 'text/javascript';
        s.src="http://fanyi.youdao.com/web2/seed.js?'+Date.parse(new Date())";
        content.document.body.insertBefore(s, content.document.body.firstChild);
     },
    image: ""
});
menu = PageMenu({
    label: "页面操作·更多",
    position:2,
    condition: "normal",
    image: ""
});
menu([{
// GBK--UTF-8
    label: "GBK <-> UTF-8",
    position:1,
    image: "",
    oncommand: function() {
        var charset = gBrowser.mCurrentBrowser._docShell.charset;
        BrowserSetForcedCharacterSet(charset == "UTF-8" ? "GBK" : "UTF-8");
    }
},{
    label: "繁体转简体",
    image: "",
    oncommand: function() {
        content.document.documentElement.appendChild(content.document.createElement("script")).src = "http://tongwen.openfoundry.org/NewTongWen/tools/bookmarklet_cn2.js";
        content.document.documentElement.appendChild(content.document.createElement("style")).textContent = 'body { font-family: "微软雅黑" }';
    }
},{
    label:"在侧栏中打开",
		oncommand:"openWebPanel(content.document.title, content.location);",
    image:""
	},{
    label:'可见区域截图',
    oncommand:function() {
    var canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
    canvas.width = content.innerWidth;canvas.height = content.innerHeight;
    var ctx =canvas.getContext("2d");ctx.drawWindow(content,content.pageXOffset, content.pageYOffset, canvas.width, canvas.height, "rgb(255,255,255)");
    saveImageURL(canvas.toDataURL(), content.document.title + ".png", null, null, null, null, document);},
    image:""
    },{
    label:'所有区域截图',
    oncommand:function() {
    var canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
    canvas.width = content.document.documentElement.scrollWidth;
    canvas.height = content.document.documentElement.scrollHeight;
    var ctx = canvas.getContext("2d");ctx.drawWindow(content, 0, 0, canvas.width, canvas.height, "rgb(255,255,255)");saveImageURL(canvas.toDataURL(), content.document.title + ".png", null, null, null, null, document);},
    image:""
},{
    label: "查找此页脚本",
    image: "",
    url: "https://greasyfork.org/zh-CN/scripts/search?q=%HOST%"
},{
                                label : '列出所有图片',
                                subdir : '',
                                image :"",
                                oncommand :function () {
                                gBrowser.loadURI("javascript:outText='';for(i=0;i<document.images.length;i++){if(outText.indexOf(document.images%5Bi%5D.src)==-1){outText+='<tr><td><img%20src='+document.images%5Bi%5D.src+'></td><td>'+document.images%5Bi%5D.height+'</td><td>'+document.images%5Bi%5D.width+'</td><td>'+document.images%5Bi%5D.src+'</td></tr>'}};if(outText!=''){imgWindow=window.open('','imgWin','width=800,height=600');imgWindow.document.write%20('<table%20border=1%20cellpadding=10><tr><th>Image</th><th>Height</th><th>Width</th><th>URL</th></tr>'+outText+'</table>');imgWindow.document.close()}else{alert('No%20images!')}");
                                },
                        },{
    label : '明文显示密码',
    subdir : '',
    image : "",
    oncommand : function () {
        gBrowser.loadURI("javascript:(function()%7Bvar%20IN,F;IN=document.getElementsByTagName('input');for(var%20i=0;i<IN.length;i++)%7BF=IN%5Bi%5D;if(F.type.toLowerCase()=='password')%7Btry%7BF.type='text'%7Dcatch(r)%7Bvar%20n,Fa;n=document.createElement('input');Fa=F.attributes;for(var%20ii=0;ii<Fa.length;ii++)%7Bvar%20k,knn,knv;k=Fa%5Bii%5D;knn=k.nodeName;knv=k.nodeValue;if(knn.toLowerCase()!='type')%7Bif(knn!='height'&&knn!='width'&!!knv)n%5Bknn%5D=knv%7D%7D;F.parentNode.replaceChild(n,F)%7D%7D%7D%7D)()")
    },
},{
				label :'在线PS+特殊字型+动态+美图',
				image:"",
				oncommand:function () {gBrowser.addTab("http://www.uupoop.com/");}
}]);

page([{
    label: "粘贴并确定",
    condition: "input",
    position : 1,
    oncommand: function(event) {
        goDoCommand("cmd_paste");
        window.QueryInterface(Ci.nsIInterfaceRequestor)
            .getInterface(Ci.nsIDOMWindowUtils)
            .sendKeyEvent("keypress", KeyEvent.DOM_VK_RETURN, 0, 0);
}
},{ 
    label: "页面稍后阅读",
    id: 'context-pocket',clone :false,
    insertAfter: "context-bookmark",
    image: ""
},{
    label: "链接稍后阅读",
    id: 'context-savelinktopocket',clone :false,
    insertAfter: "context-copylinktext",
    image: ""
}]);

page({
    label: "PlaywithPot",
    tooltiptext: "调用Pot来播放，前提是m3u默认是pot播放",
    condition: "link noimage",
    image: "",
    onclick: function () {
			var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
			try {
				var path = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getCharPref("browser.cache.disk.parent_directory") + "\\" + new Date().getTime() + ".m3u";
				file.initWithPath(path);
			} catch (e) {
				var path = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfLD", Components.interfaces.nsILocalFile).path + "\\" + new Date().getTime() + ".m3u";
			}
			file.initWithPath(path);
			
			var x = gContextMenu.mediaURL || gContextMenu.linkURL;
			//alert("x='"+x+"' path='"+path+"'");
			Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Ci.nsIWebBrowserPersist).saveURI(Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(x, null, null), null, null, null, null, null, file, null);
			
			setTimeout(function () {
				file.launch();
			}, 300);
		}
});

// 图像
menu = PageMenu({
    label: "图片操作",
    position: 3,
    condition: "image",
});
menu([{
    label: "复制图像的 Base64",
    text: "%IMAGE_BASE64%",
    condition: "image"
},{
    label: "Base64转图片地址",
    url: "http://www.atool.org/img2base64.php",
    condition: "image"
}, {
    label: "四引擎搜图",
    condition: "image",
    image: "http://www.tineye.com/favicon.ico",
    oncommand: function() {
        var url = encodeURIComponent(gContextMenu.mediaURL || gContextMenu.imageURL || gContextMenu.bgImageURL);
        gBrowser.addTab('https://www.google.com/searchbyimage?safe=off&image_url=' + url);
        gBrowser.addTab('http://www.tineye.com/search/?pluginver=firefox-1.0&sort=size&order=desc&url=' + url);
        gBrowser.addTab('http://stu.baidu.com/i?rt=0&rn=10&ct=1&tn=baiduimage&objurl=' + url);
        gBrowser.addTab('http://pic.sogou.com/ris?query=' + url);
    }
}, {
    label: "打开图像RAR",
    condition: "image",
    image: "moz-icon://file:///C:/Program Files/2345Soft/HaoZip/HaoZip.exe?size=16",
    oncommand: function() {
        var imageUrl = (gContextMenu.mediaURL || gContextMenu.imageURL);
        imageUrl = imageUrl.replace(/\.jpg\.thumb\.jpg$/i, '.jpg');
        var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
			try {
				var path = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getCharPref("browser.cache.disk.parent_directory") + "\\" + new Date().getTime() + ".zip";
				file.initWithPath(path);
			} catch (e) {
				var path = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfLD", Components.interfaces.nsILocalFile).path + "\\" + new Date().getTime() + ".zip";
			}
			file.initWithPath(path);
			Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Ci.nsIWebBrowserPersist).saveURI(Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(imageUrl, null, null), null, null, null, null, null, file, null);
        setTimeout(function() {
            file.launch();
        }, 100);
    }
}]);


page([{ 
    id: 'context-searchfield',clone :false,
    image: ""
},{ 
    id: 'context-bookmarklink',clone :false,
    insertAfter: "context-copylinktext",
    image: ""
}]);

//隐藏相同项。必须，不能删除
function syncHidden(event) {
	Array.slice(event.target.children).forEach(function(elem){
		var command = elem.getAttribute('command');
		if (!command) return;
		var original = document.getElementById(command);
		if (!original) {
				elem.hidden = true;
				return;
		};
		elem.hidden = original.hidden;
		elem.collapsed = original.collapsed;
		elem.disabled = original.disabled;
	});
};